package pojo.wizards;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;


import pojo.utils.I18n;
import pojo.utils.UiUtils;

public class PathWizardPage extends WizardPage {

	private boolean inited = false;
	private TabFolder tabFolder; // 代码生成策略选项卡
	private Text baseModelPackageName;
	private Text baseModelOutputDir;

	protected PathWizardPage(String pageName) {
		super(pageName);
		setTitle("路径配置信息");
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(1, false);
		container.setLayout(layout);

		tabFolder = UiUtils.createTabFolder(this, container);
		tabFolder.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
		
		
		TabItem customTabItem = new TabItem(tabFolder, SWT.NULL);
		customTabItem.setText("Custom");
		Composite customTabContainer = new Composite(tabFolder, SWT.NULL);
		GridLayout customTabLayout = new GridLayout(3, false);
		customTabContainer.setLayout(customTabLayout);
		customTabLayout.marginTop = 15;
		customTabLayout.horizontalSpacing = 5;
		customTabLayout.verticalSpacing = 5;
		customTabItem.setControl(customTabContainer);
		baseModelPackageName = UiUtils.createText(customTabContainer, I18n.makeCodeUI_modelPackageNameLabel);
		baseModelOutputDir = UiUtils.createJavaText(getShell(), this, customTabContainer);

		TabItem jfinalTabItem = new TabItem(tabFolder, SWT.NULL);
		jfinalTabItem.setText("JFinal");
		Composite jfinalTabContainer = new Composite(tabFolder, SWT.NULL);
		GridLayout jfinalTabLayout = new GridLayout(3, false);
		jfinalTabContainer.setLayout(jfinalTabLayout);
		jfinalTabLayout.marginTop = 30;
		jfinalTabLayout.marginRight = 20;
		jfinalTabLayout.marginLeft = 20;
		jfinalTabLayout.horizontalSpacing = 10;
		jfinalTabLayout.verticalSpacing = 10;
		jfinalTabItem.setControl(jfinalTabContainer);
		
		
		setControl(container);
		inited = true;
	}
	

	@Override
	public void setVisible(boolean visible) {
		UiUtils.Validator.validatorEmptyString(this);
		super.setVisible(visible);
	}
	
	public TabFolder getTabFolder() {
		return tabFolder;
	}
	
	public boolean isInited() {
		return inited;
	}

	public Text getBaseModelPackageName() {
		return baseModelPackageName;
	}
	
	public String getBaseModelPackageName_() {
		return this.getBaseModelPackageName().getText().trim();
	}

	public String getBaseModelOutputDir_() {
		Text t = this.getBaseModelOutputDir();
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource res = root.findMember(new Path(t.getText().trim()));
		if (res.exists() && (res instanceof IContainer)) {
			return res.getLocationURI().toString().replace("file:", "");
		}
		return null;
	}
	public Text getBaseModelOutputDir() {
		return baseModelOutputDir;
	}


}
